package com.example.demo.util;

import com.example.demo.model.UserInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONUtils {

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();

        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setAge(19);
        userInfo.setName("zhangsan");

        // 对象转成 json
        String s = objectMapper.writeValueAsString(userInfo);
        System.out.println(s);

        // json 转成对象
        UserInfo userInfo1 = objectMapper.readValue(s, UserInfo.class);
        System.out.println(userInfo1);
    }
}
